# -*- coding: utf-8 -*-
from . import BaseController
from backend.libs import myjson as json
from backend.models import Element, Category, Option, OptionElement, VideoTypeCategoryElement, VideoType


class List(BaseController):

    def get(self):
        ele = Element.objects
        
        state = self.int_argument('state', 0)
        ele = ele.filter(state=state)
        ele = ele.order_by('show_order')
        number_per_page = self.int_argument('pageSize', 50)
        paginator = self.pagination(ele.all(), number_per_page=number_per_page)
        return self.render_json(data=paginator)


class Detail(BaseController):

    def get(self):
        id = self.int_argument('id', 0)
        if id <= 0:
            return self.render_json(code=1001, msg='参数错误')
        ele = Element.objects.filter(id=id).first()
        
        return self.render_json(data=ele.to_detail_dict())


class ListAll(BaseController):

    def get(self):
        elements = Element.objects.filter(state=0).order_by('show_order').all()
        data = [ {'label': e.name, 'value': e.id} for e in elements ]
        return self.render_json(data={'elements': data})


class BaseElementController(BaseController):

    def set_element_properties(self, ele, data):
        ele.name = data.get('name')
        ele.unit = data.get('unit')
        ele.show_order = data.get('show_order')
        ele.price = data.get('price')
        ele.state = data.get('state', 0)
        ele.type = data.get('type')
        ele.level = data.get('level')
        ele.desc = data.get('desc')
        ele.url = data.get('url', '')
        # ele.category_id = data.get('category_id')
        ele.save()
        # VideoTypeCategoryElement.objects.filter(element=ele).delete()
        category_id = data.get('category_id')
        if category_id:
            category = Category.objects.filter(id=category_id).first()
            if category:
                category.elements.add(ele)
                category.save()
                # video_type_id = data.get('video_type_id')
                # if video_type_id and int(video_type_id) > 0:
                #     video_type = VideoType.objects.filter(id=video_type_id).first()
                #     if video_type:
                #         vte = VideoTypeCategoryElement()
                #         vte.name = ''
                #         vte.video_type = video_type
                #         vte.category = category
                #         vte.element = ele
                #         vte.show_order = data.get('relation_show_order', 1)
                #         vte.save()
        # category_options = data.get('category_options', [])
        # for d in category_options:
        #     option_id = d['id']
        #     if option_id:
        #         option = Option.objects.filter(id=option_id).first()
        #         if option:
        #             oe = OptionElement()
        #             oe.option = option
        #             oe.element = ele
        #             oe.save()
        return ele

class CreateElement(BaseElementController):

    def post(self):
        data = json.loads(self.request.body)
        if not data['name']:
            return self.render_json(code=1001, msg='元素名称不能为空')
        ele = Element()
        ele = self.set_element_properties(ele, data)
        
        return self.render_json(data=ele.to_dict())


class UpdateElement(BaseElementController):

    def post(self):
        data = json.loads(self.request.body)
        if not data['name']:
            return self.render_json(code=1001, msg='标签名称不能为空')
        ele_id = data.get('id', -1)
        if ele_id <= 0:
            return self.render_json(code=1001, msg='标签名称不能为空')
        ele = Element.objects.filter(id=ele_id).first()
        ele = self.set_element_properties(ele, data)
        return self.render_json(data=ele.to_dict())